package com.bnz.service.impl;

import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;

    @Override
    public List<Student> findAll() {

        List<Student> students=studentMapper.selectByExample(null);

        for (Student student : students) {
           Classes classes= classesMapper.selectByPrimaryKey(student);
           student.setCname(classes.getCname());
        }

        return students;
    }

    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        Example example=new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();

        List<Student> students=studentMapper.selectByExample(example);

        for (Student student : students) {
            Classes classes=classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }

        Page<Student> studentPage= (Page<Student>) students;
        return new PageResult<>(studentPage.getTotal(),
                studentPage.getPages(),
                page,
                studentPage.getResult());

    }

    @Override
    public PageResult<String> search(Integer page, Integer pageSize, StudentVo studentVo) {

        PageHelper.startPage(page,pageSize);

        Example example=new Example(Student.class);

        Example.Criteria criteria=example.createCriteria();
        if(studentVo != null){
            if(!StringUtils.isEmpty(studentVo.getSname())){
                criteria.andLike("sname","%" + studentVo.getSname() + "%");
            }
            if(!StringUtils.isEmpty(studentVo.getAddr())){
                criteria.andLike("addr","%" + studentVo.getAddr() + "%");
            }
            if(studentVo.getCid() != null && studentVo.getCid() != 0){
                criteria.andEqualTo("cid",studentVo.getCid());
            }
        }

        List<Student> students=studentMapper.selectByExample(example);

        for (Student student : students) {
            Classes classes=classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }

        Page<Student> studentPage= (Page<Student>) students;

        return new PageResult(studentPage.getTotal(),
                studentPage.getPages(),
                page,
                studentPage.getResult());
    }

    @Override
    public void add(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    @Override
    public void delete(Integer id) {
        studentMapper.deleteByPrimaryKey(id);
    }
}
